//获取应用实例
import Promise from '../../../utils/es6-promise/es6-promise'
import regeneratorRuntime from '../../../utils/regenerator-runtime/runtime'
const utils = require('../../../utils/util.js');
const app = getApp()

Page({
  data: {
    type:2,
    url:"bankList",
    cityUrl:"/api/Citys/getCitys",
    token:null,
    cityList:[],
    cityname:"",
    cityid:null,
    cityindex:null,
    bankListUrl:"/api/Bank/blist",
    bankList:[],
    jumpUrl:"../detail/index",
    isCityDisable:false,
    isHide:false
  },
  onShow:function(){
    if(this.data.isHide && this.data.cityname!=app.globalData.cityname){
      this.setData({
        isCityDisable:false
      })
      this.onLoad();
    }
    this.setData({
      isHide:false
    })
  },
  onHide:function(){
    this.setData({
      isHide:true
    })
  },
  onLoad: async function(){
    if(!app.globalData.userInfo){
      wx.redirectTo({
        url: '../start/index'
      })
    }
    this._showLoading();
    var token = await app.getToken();
    this.setData({
      token:token
    })
    var result=await this._getCityList(); 
    if(result.state){
      var list=result.list;
      this.setData({
        cityList:list
      })
    }
    else{
      wx.showToast({
        title:result.msg,
        icon:"none",
        duration:1000
      })
      return;
    }
    var cityNow=app.globalData.cityname?{state:true,data:app.globalData.cityname}:await this._getNowCity();
    if(cityNow.state){
      var data=cityNow.data;
      this.setData({
        cityname:data
      })
      app.globalData.cityname=data;
    }
    else{
      this.setData({
        cityname:"获取失败",
        isCityDisable:true
      })
      this._hideLoading();
      /* wx.showToast({
        title:cityNow.msg,
        icon:"none",
        duration:1000
      }) */
      return;
    }
    var cityname=this.data.cityname;
    var activeObj=this.data.cityList.find(it=>it.cityname==cityname);
    var activeIndex=this.data.cityList.findIndex(it=>it.cityname==cityname);
    var cityid=activeObj?activeObj.cityid:null;
    if(cityid==null){
      this.setData({
        bankList:[]
      })
      this._hideLoading();
    }
    else{
      this.setData({
        cityid:cityid,
        cityindex:activeIndex
      })
      this._setBankList();
    }
  },
  _setBankList: async function(){
    var bankList=await this._getBankList();
      this._hideLoading();
      if(bankList.state){
        var list=bankList.list;
        this.setData({
          bankList:list
        })
      }
      else{
        wx.showToast({
          title:bankList.msg,
          icon:"none",
          duration:1000
        })
        return;
      }
  },
  _getCityList:function(){
    var token=this.data.token;
    var p = new Promise((resolve,reject)=>{
        //做一些异步操作
        app.request(this.data.cityUrl,{token:token},res=>{
          console.log(res);
          var data=res.data;
          /* var data=[
            {
              cityid:1,
              cityname:"武汉"
            },
            {
              cityid:2,
              cityname:"上海"
            }
          ] */
          if(data.state){
            resolve({state:true,list:data.data});
            //resolve({state:true,list:data.data});
          }
          else{
            resolve({state:false,msg:data.msg});          
          }
          //resolve({state:res.data.state,data:res.data.data});      
      },error=>{
        resolve({state:false,msg:"获取失败，请重试"});
      })
    });
    return p;
  },
  _getNowCity:function(){
    //return {state:true,city:"上海"}
    var p = new Promise((resolve,reject)=>{
        //做一些异步操作
        wx.getLocation({
          type: 'wgs84',
          success:res=> {
            console.log(res);
            const latitude = res.latitude
            const longitude = res.longitude
            wx.request({
              url:'https://api.map.baidu.com/geocoder/v2/?ak='+app.globalData.baiduAppKey+'&location=' + latitude + ',' + longitude+ '&output=json&coordtype=wgs84ll',
              data: {},
              header: {
                'Content-Type': 'application/json'
              },
              success: function (res) {
                var city=res.data.result.addressComponent.city;
                city=city.replace("市","");
                console.log(res.data.result.addressComponent.city);
                resolve({state:true,data:city});
              },
              fail: function () {
                // fail
                resolve({state:false,msg:"定位失败，请手动选择"});
              }
            })
          },
          fail:res=> {
            console.log(res);
            resolve({state:false,msg:"定位失败，请手动选择"});
          }
        })
        //resolve({state:true,list:data.data});
    });
    return p;
  },
  _getBankList:function(){
    var token=this.data.token;
    var cityid=this.data.cityid;
    var p = new Promise((resolve,reject)=>{
        //做一些异步操作
        var dataset={token:token,type:this.data.type};
        if(this.data.cityid) dataset.cityid=this.data.cityid;
        app.request(this.data.bankListUrl,dataset,res=>{
          console.log(res);
          var data=res.data;
          /* var data=[
            {
              cityid:1,
              cityname:"武汉"
            },
            {
              cityid:2,
              cityname:"上海"
            }
          ] */
          if(data.state){
            resolve({state:true,list:data.data});
            //resolve({state:true,list:data.data});
          }
          else{
            resolve({state:false,msg:data.msg});          
          }
          //resolve({state:res.data.state,data:res.data.data});      
      },error=>{
        resolve({state:false,msg:"获取失败，请重试"});
      })
    });
    return p;
  },
  jumpDetail:function(e){
    var id=e.currentTarget.dataset.id;
    var name=e.currentTarget.dataset.name;
    wx.navigateTo({
      url: this.data.jumpUrl+"?id="+id+"&name="+name
    })
  },
  bindCityChange:function(e){
    var id=this.data.cityList[e.detail.value].cityid;
    var name=this.data.cityList.find(it=>it.cityid==id).cityname;
    var cityindex=this.data.cityList.findIndex(it=>it.cityid==id);
    this.setData({
      cityid:id,
      cityname:name,
      cityindex:cityindex,
      isCityDisable:false
    })
    app.globalData.cityname=name;
    this._showLoading();
    this._setBankList();
  },
  _showLoading:function(){
    wx.showLoading({
      title:"加载中..."
    })
  },
  _hideLoading:function(){
    wx.hideLoading();
  }
})
